<?php

namespace App\HttpController\Api;

use App\Think\Config;
use App\Think\Db;
use App\Model\Bonus as BonusLib;

/**
 * 活动红利接口Bonus
 */
class Bonus extends ApiBase
{
	protected $noNeedLogin = ['bonus_list','register'];

	/**
     * 活动列表
     */
    public function bonus_list(){
        $list = $res = [];
        $list = Config::get('Project.bonus');
        foreach ($list as $k => $v) {
            if(@$v['view'] == 1) $res[] = $v;
        }
        return $this->success(__('success'), ['list'=>$res]);
    }

    /**
     * 注册送彩金
     * ApiMethod (POST)
     * @param string $user_id 用户ID
     */
    public function register(){
        $user_id  = $this->input('user_id');
        if (!$user_id) return $this->error(__('请求参数不完整'));
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='register'", 'id');
        if($bonus_id>0) return $this->error(__('请不要重复参加此活动'));
        BonusLib::register($user_id);
        return $this->success(__('申请成功'));
    }


    /**
     * 首次充值-获取信息
     * ApiMethod (POST)
     */
    public function first_recharge_info(){
        $max_bonus = 600;
        $user_id = $this->auth->id;
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='first_recharge'", 'id');
        $has_apply = $bonus_id>0 ? 1 : 0;

        $money   = (float)Db::name('trade_recharge')->where("user_id='{$user_id}' AND status=1")->order('id ASC')->value('money');
        $user_info = Db::name('user u')->field('ul.*')->join('user_level ul', 'u.level=ul.id', 'LEFT')->where("u.id='{$user_id}'")->find();
        $inc_money = @$user_info['ac_first_recharge']*$money/100;
        $inc_money = $inc_money > $max_bonus ? $max_bonus : $inc_money;
        return $this->success(__('success'), ['has_apply'=>$has_apply, 'money'=>(string)$money, 'money_bonus'=>(string)$inc_money, 'max_bonus'=>(string)$max_bonus, 'rate'=>@$user_info['ac_first_recharge']]);
    }
    /**
     * 首次充值-申请
     * ApiMethod (POST)
     * @param string $plat_type 平台类型key|default=不限,sport=体育赛事,esport=电子竞技
     */
    public function first_recharge(){
        $plat_type = $this->input('plat_type');
        $user_id   = $this->auth->id;
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='first_recharge'", 'id');
        if($bonus_id>0) return $this->error(__('请不要重复参加此活动'));
        $money   = Db::name('trade_recharge')->where("user_id='{$user_id}' AND status=1")->order('id ASC')->value('money');
        if(empty($money)) return $this->error(__('请先进行首次充值'));
        if($plat_type != 'default' && empty(platform_name($plat_type)) ) return $this->error(__('请选择合适的游戏场馆'));
        BonusLib::first_recharge($user_id, $money, $plat_type);
        return $this->success(__('申请成功'));
    }


    /**
     * 二次充值-获取信息
     * ApiMethod (POST)
     */
    public function second_recharge_info(){
        $max_bonus = 300;
        $user_id = $this->auth->id;
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='second_recharge'", 'id');
        $has_apply = $bonus_id>0 ? 1 : 0;

        $money   = (float)Db::name('trade_recharge')->page(2,1)->where("user_id='{$user_id}' AND status=1")->order('id ASC')->value('money');
        $user_info = Db::name('user u')->field('ul.*')->join('user_level ul', 'u.level=ul.id', 'LEFT')->where("u.id='{$user_id}'")->find();
        $inc_money = @$user_info['ac_second_recharge']*$money/100;
        $inc_money = $inc_money > $max_bonus ? $max_bonus : $inc_money;
        return $this->success(__('success'), ['has_apply'=>$has_apply, 'money'=>(string)$money, 'money_bonus'=>(string)$inc_money, 'max_bonus'=>(string)$max_bonus, 'rate'=>(string)$user_info['ac_second_recharge']]);
    }
    /**
     * 二次充值-申请
     * ApiMethod (POST)
     * @param string $plat_type 平台类型key|default=不限,sport=体育赛事,esport=电子竞技
     */
    public function second_recharge(){
        $plat_type = $this->input('plat_type');
        $user_id   = $this->auth->id;
        $bonus_id  = db_get_field('trade_bonus', "user_id = '{$user_id}' AND bonus_type = 'second_recharge'", 'id');
        if($bonus_id>0) return $this->error(__('请不要重复参加此活动'));
        $money   = Db::name('trade_recharge')->page(2,1)->where("user_id='{$user_id}' AND status=1")->order('id ASC')->value('money');
        if(empty($money)) return $this->error(__('请先进行第二次充值'));
        if($plat_type != 'default' && empty(platform_name($plat_type)) ) return $this->error(__('请选择合适的游戏场馆'));
        BonusLib::second_recharge($user_id, $money, $plat_type);
        return $this->success(__('申请成功'));
    }
    /**
     * 充值/银行卡送彩金
     * ApiMethod (POST)
     */
    public function bank_recharge(){
        $user_id = $this->auth->id;
        $row = Db::name('trade_recharge')->field('order_sn,money')->where("user_id='{$user_id}' AND status=1 AND paytype='bank'")->order('id DESC')->find();
        if(empty($row) || empty($row['money'])) return $this->error(__('请先进行银行卡充值'));
        // $ext_json = json_encode(['order_sn'=>$row['order_sn']]);
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='bank_recharge' AND ext_json->'$.order_sn'='{$row['order_sn']}' ", 'id');
        if($bonus_id>0) return $this->error(__('您已经领取过本次充值红利'));
        BonusLib::bank_recharge($user_id, $row['money'], $row['order_sn']);
        return $this->success(__('申请成功'));
    }

    /**
     * 百万豪礼申请
     * ApiMethod (POST)
     * @param string $gift 礼物名
     * @param string $addr 收货地址|省+市+区+地址
     */
    public function gift(){
        $plat_type  = $this->input('plat_type');
        $gift  = $this->input('gift');
        $addr  = $this->input('addr');

        $user_id = $this->auth->id;
        $bonus_type = 'gift';
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='$bonus_type'", 'id');
        if($bonus_id>0) return $this->error(__('请不要重复参加此活动'));
        Db::name('trade_bonus')->insert([
            'order_sn'   => order_sn(),
            'bonus_type' => $bonus_type,
            'money'      => 0,
            'user_id'    => $user_id,
            'ext_json'   => json_encode(['name'=>$this->auth->nickname, 'phone'=>$this->auth->mobile, 'gift'=>$gift, 'addr'=>$addr]),
            'memo'       => bonus_name($bonus_type),
            'createtime' => time()
        ]);
        return $this->success(__('申请成功'));
    }


    /**
     * 邀请历史记录
     * ApiMethod (POST)
     */
    public function invite_log(){
        $user_id = $this->auth->id;
        $bonus_type = 'invite';
        $total = Db::name('trade_bonus')->field('sum(1) total_num, sum(money) total_money')->where("bonus_type='$bonus_type' AND user_id='{$user_id}'")->find();
        return $this->success(__('success'), ['total'=>$total]);
    }
    /**
     * 邀请新人/自动发放
     * ApiMethod (POST)
     */
    public function invite(){
        $user_id = $this->auth->id;
        $bonus_type = 'invite';
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='$bonus_type'", 'id');
        if($bonus_id>0) return $this->error(__('请不要重复参加此活动'));
        $user_info = Db::name('user u')->field('ul.*')->join('user_level ul', 'u.level=ul.id', 'LEFT')->where("u.id='{$user_id}'")->find();
        $inc_money = @$user_info['ac_invite'];
        Db::name('trade_bonus')->insert([
            'order_sn'   => order_sn(),
            'bonus_type' => $bonus_type,
            'money'      => $inc_money,
            'user_id'    => $user_id,
            'ext_json'   => json_encode([]),
            'memo'       => bonus_name($bonus_type),
            'createtime' => time()
        ]);
        return $this->success(__('申请成功'));
    }

    /**
     * 每月红利/自动发放
     * ApiMethod (POST)
     */
    /*public function month(){
        $user_id = $this->auth->id;
        $bonus_type = 'month';
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='$bonus_type'", 'id');
        if($bonus_id>0) return $this->error(__('请不要重复参加此活动'));
        $user_info = Db::name('user u')->field('ul.*')->join('user_level ul', 'u.level=ul.id', 'LEFT')->where("u.id='{$user_id}'")->find();
        $inc_money = @$user_info['ac_month'];
        Db::name('trade_bonus')->insert([
            'order_sn'   => order_sn(),
            'bonus_type' => $bonus_type,
            'money'      => $inc_money,
            'user_id'    => $user_id,
            'ext_json'   => json_encode([]),
            'memo'       => bonus_name($bonus_type),
            'createtime' => time()
        ]);
        return $this->success(__('申请成功'));
    }*/

    /**
     * 会员升级
     * ApiMethod (POST)
     */
    /*public function upgrade(){
        $user_id = $this->auth->id;
        $level_up   = $this->auth->level+1;

        $bonus_date = Db::name('trade_bonus')->where("user_id='{$user_id}' AND bonus_type='upgrade'")->order('createtime DESC')->value('createtime');
        if(date('Y-m-d') == date('Y-m-d',$bonus_date)) return $this->error(__('每日仅能升级一次'));
        //是否已经领取过等级奖励
        // $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='upgrade' AND ext_json->'$.level'='{$level_up}' ", 'id');
        // if($bonus_id>0) return $this->success(__('升级成功'));
        BonusLib::upgrade($user_id);
        return $this->success(__('升级成功'));
    }*/
    /**
     * 生日礼金
     * ApiMethod (POST)
     */
    public function birth(){
        $user_id = $this->auth->id;
        $year = date('Y');
        $bonus_id = db_get_field('trade_bonus', "user_id='{$user_id}' AND bonus_type='birth' AND ext_json->'$.year'='{$year}' ", 'id');
        if($bonus_id>0) return $this->error(__('请不要重复参加此活动'));
        $user_info = Db::name('user u')->field('u.birthday,u.jointime,ul.*')->join('user_level ul', 'u.level=ul.id', 'LEFT')->where("u.id='{$user_id}'")->find();
        if($user_info['ac_birth']==0 || empty($user_info['ac_birth'])) return $this->error(__('当前等级没有生日礼金奖励'));
        if( (time()-$user_info['jointime'])<86400*90 ) return $this->error(__('加入平台的时间需要超过三个月'));
        if(date('m-d',time()) != date("m-d",strtotime("{$user_info['birthday']} 00:00:00"))) return $this->error(__('请在您生日当天申请该活动'));
        BonusLib::birth($user_id);
        return $this->success(__('申请成功'));
    }



}